﻿namespace HutongGames.PlayMakerEditor
{
    using System;
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;

    public class FsmTemplateSelector : EditorWindow
    {
        private FsmTemplate afterSelected;
        private FsmTemplate beforeSelected;
        private int previousSelectedCateogry;
        private FsmTemplate prevTemplate;
        private Vector2 scrollPosition;
        [SerializeField]
        private int selectedCategory;
        private FsmTemplate selectedTemplate;

        private static void AddTemplateToFsm()
        {
            if (FsmEditor.SelectedFsm != null)
            {
                EditorCommands.PasteTemplate(FsmGraphView.GetViewCenter());
            }
        }

        private void DoBottomPanel()
        {
            FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
            if (this.selectedTemplate == null)
            {
                GUI.enabled = false;
                GUILayout.TextField("Category...", new GUILayoutOption[0]);
                EditorGUILayout.TextArea("Description...", new GUILayoutOption[] { GUILayout.MinHeight(44f) });
            }
            else
            {
                bool changed = GUI.changed;
                GUI.changed = false;
                this.selectedTemplate.Category = EditorGUILayout.TextField(this.selectedTemplate.Category, new GUILayoutOption[0]);
                this.selectedTemplate.fsm.Description = EditorGUILayout.TextArea(this.selectedTemplate.fsm.Description, FsmEditorStyles.TextArea, new GUILayoutOption[] { GUILayout.MinHeight(44f) });
                if (GUI.changed)
                {
                    EditorUtility.SetDirty(this.selectedTemplate);
                    FsmEditorUtility.BuildTemplateList();
                }
                else
                {
                    GUI.changed = changed;
                }
                if ((Event.current.type != EventType.Used) && (Event.current.clickCount == 2))
                {
                    AddTemplateToFsm();
                    Event.current.Use();
                }
            }
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            if (FsmEditor.SelectedFsm == null)
            {
                GUI.enabled = false;
            }
            if (GUILayout.Button("Paste Template", new GUILayoutOption[0]))
            {
                AddTemplateToFsm();
            }
            GUILayout.EndHorizontal();
            EditorGUILayout.Space();
        }

        private void DoTemplateHierarchyGUI()
        {
            this.scrollPosition = GUILayout.BeginScrollView(this.scrollPosition, new GUILayoutOption[0]);
            for (int i = 0; i < FsmEditorUtility.TemplateCategories.Count; i++)
            {
                this.previousSelectedCateogry = this.selectedCategory;
                if (GUILayout.Toggle(this.selectedCategory == i, FsmEditorUtility.TemplateCategories[i], "Button", new GUILayoutOption[0]))
                {
                    this.selectedCategory = i;
                    if (this.previousSelectedCateogry != this.selectedCategory)
                    {
                        this.selectedTemplate = null;
                    }
                }
                if (this.selectedCategory == i)
                {
                    this.DoTemplateSelectorGUI(FsmEditorUtility.TemplateCategories[i]);
                }
            }
            GUILayout.EndScrollView();
        }

        private void DoTemplateSelectorGUI(string categoryName)
        {
            foreach (KeyValuePair<FsmTemplate, string> pair in FsmEditorUtility.TemplateCategoryLookup)
            {
                if (pair.Value == categoryName)
                {
                    FsmTemplate key = pair.Key;
                    GUIStyle actionItem = FsmEditorStyles.ActionItem;
                    if (this.selectedTemplate == key)
                    {
                        actionItem = FsmEditorStyles.ActionItemSelected;
                    }
                    if (GUILayout.Button(key.name, actionItem, new GUILayoutOption[0]))
                    {
                        this.SelectTemplate(key);
                    }
                    if (key == this.selectedTemplate)
                    {
                        this.beforeSelected = this.prevTemplate;
                    }
                    if (this.prevTemplate == this.selectedTemplate)
                    {
                        this.afterSelected = key;
                    }
                    this.prevTemplate = key;
                }
            }
        }

        private void HandleKeyboardInput()
        {
            if (GUIUtility.keyboardControl == 0)
            {
                int controlID = GUIUtility.GetControlID(FocusType.Keyboard);
                if (Event.current.GetTypeForControl(controlID) == EventType.KeyDown)
                {
                    switch (Event.current.keyCode)
                    {
                        case KeyCode.UpArrow:
                            Event.current.Use();
                            this.SelectPrevious();
                            GUIUtility.ExitGUI();
                            return;

                        case KeyCode.DownArrow:
                            Event.current.Use();
                            this.SelectNext();
                            GUIUtility.ExitGUI();
                            return;
                    }
                }
            }
        }

        private void OnEnable()
        {
            base.title = "Templates";
            base.minSize = new Vector2(200f, 250f);
            base.autoRepaintOnSceneChange = true;
            FsmEditorUtility.BuildTemplateList();
            this.SelectTemplate(null);
        }

        private void OnGUI()
        {
            if (FsmEditorGUILayout.ToolWindowsCommonGUI(this))
            {
                if (((Event.current.type == EventType.MouseDown) && (Event.current.button == 0)) && (GUIUtility.hotControl == 0))
                {
                    GUIUtility.keyboardControl = 0;
                }
                this.HandleKeyboardInput();
                this.DoTemplateHierarchyGUI();
                this.DoBottomPanel();
            }
        }

        private void SelectNext()
        {
            if ((this.selectedTemplate != null) && (this.afterSelected != null))
            {
                this.SelectTemplate(this.afterSelected);
            }
        }

        private void SelectPrevious()
        {
            if ((this.selectedTemplate != null) && (this.beforeSelected != null))
            {
                this.SelectTemplate(this.beforeSelected);
            }
        }

        private void SelectTemplate(FsmTemplate template)
        {
            this.selectedTemplate = template;
            FsmEditor.SelectTemplate(template);
        }
    }
}

